angular.module('mainApp')
    .constant('paginationConfig', {
      maxSize: 5,  // 最多显示多少页
      rotate: false  // 当前页是否显示在中间位置
    })
    .directive('paginationComponent', ['paginationConfig', function (paginationConfig) {

      return {
        restrict: 'EA',
        scope: {
          totalItems: '=', // 总条数
          currentPage: '=', // 当前页
          pageSize: '=', // 最大页数
          selectPage: '&' //
        },

        templateUrl: 'modules/component/pagination/pagination.html',
        replace: true,
        controller: function ($scope, $element, $attrs) {
          console.log($scope);
          /**
           * 创建页对象
           * @param index 页码
           * @param text 显示的文字
           * @param active 是否激活
           * @returns {{text: *, index: *, active: *}}
           */
          var makePage = function (index, text, active) {
            return {
              text: text,
              index: index,
              active: active
            }
          };
          /**
           * 计算总页数
           */
          var calculateTotalPages = function () {
            //console.log($scope.totalItems);
            return Math.ceil($scope.totalItems / $scope.pageSize);
          };
          /**
           * 获取页数组
           * @returns {Array}
           */

          var getPages = function () {

            var maxSize = angular.isDefined($attrs.maxSize) ? $scope.$eval($attrs.maxSize) : paginationConfig.maxSize;
            var rotate = angular.isDefined($attrs.rotate) ? $scope.$eval($attrs.rotate) : paginationConfig.rotate;
            var pages = [];
            var startPage = 1,
                endPage = $scope.totalPages;
            var isMaxSized = (angular.isDefined(maxSize) && maxSize < $scope.totalPages);
            if (isMaxSized) {
              if (rotate) {
                startPage = Math.max($scope.currentPage - Math.floor(maxSize / 2), 1);
                endPage = startPage + maxSize - 1;
                if (endPage > $scope.totalPages) {
                  endPage = $scope.totalPages;
                  startPage = endPage - maxSize + 1;
                }
              } else {
                startPage = ((Math.ceil($scope.currentPage / maxSize) - 1) * maxSize) + 1;
                endPage = Math.min(startPage + maxSize - 1, $scope.totalPages);
              }
            }
            for (var number = startPage; number <= endPage; number++) {
              var page = makePage(number, number, number === $scope.currentPage);
              pages.push(page);
            }
            if (isMaxSized && !rotate) {
              if (startPage > 1) {
                var previousPageSet = makePage(startPage - 1, '...', false);
                pages.unshift(previousPageSet);
              }
              if (endPage < $scope.totalPages) {
                var nextPageSet = makePage(endPage + 1, '...', false);
                pages.push(nextPageSet);
              }
            }
            return pages;
          };
          /**
           * 是否还有上一页
           * @returns {boolean}
           */
          $scope.noPrevious = function () {
            return $scope.currentPage === 1;
          };
          /**
           * 是否还有下一页
           * @returns {boolean}
           */
          $scope.noNext = function () {
            return $scope.currentPage === $scope.totalPages;
          };
          /**
           * 监控数据总条数
           */
          $scope.$watch('totalItems', function () {
            $scope.totalPages = calculateTotalPages();
            $scope.pages = getPages();
          });
          /**
           * 监控当前页码
           */
          $scope.$watch('currentPage', function (newVal) {
            $scope.pages = getPages();
          });
          /**
           * 监控最大页数
           */
          $scope.$watch('totalPage', function (totalPage) {
            // 如果当前页数大于最大页数，
            // 并且当前页数不等于1，后退一页
            if ($scope.currentPage > totalPage && totalPage > 0) {
              $scope.selectPage({pageIndex: $scope.currentPage - 1});
            }
          });
        }
      }
    }]);